// DlgSendLink.cpp : implementation file
//

#include "stdafx.h"
#include "Celsus.h"
#include "DlgSendLink.h"
#include "DlgPreviewHTML.h"
#include "SendMessage.h"
#include "DlgPromptMaterial.h"


// CDlgSendLink dialog

IMPLEMENT_DYNAMIC(CDlgSendLink, CDialog)

CDlgSendLink::CDlgSendLink(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgSendLink::IDD, pParent)
	, m_szURL(_T(""))
	, m_szInformation(_T(""))
	, m_szInfo(_T(""))
	, m_nMaterial(0)
{
	m_nItemID = 0;
	m_nUserID = 0;
	m_nStaffID = 0;
	m_nLibraryID = 0;
	m_nCollectionID = 0;
	m_bCheckLink = FALSE;
}

CDlgSendLink::~CDlgSendLink()
{
}

void CDlgSendLink::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT_URL, m_szURL);
	DDX_Text(pDX, IDC_EDIT_NOTES, m_szInformation);
	DDX_Text(pDX, IDC_STATIC_INFO, m_szInfo);
	DDX_Control(pDX, IDC_COMBO_COLLECTION, m_ctrlCollection);
	DDX_Radio(pDX, IDC_RADIO_MATERIAL_1, m_nMaterial);
}


BEGIN_MESSAGE_MAP(CDlgSendLink, CDialog)
	ON_CBN_SELCHANGE(IDC_COMBO_COLLECTION, &CDlgSendLink::OnCbnSelchangeComboCollection)
	ON_BN_CLICKED(IDC_BUTTON_OPEN_LINK, &CDlgSendLink::OnBnClickedButtonOpenLink)
	ON_EN_CHANGE(IDC_EDIT_URL, &CDlgSendLink::OnEnChangeEditUrl)
END_MESSAGE_MAP()


// CDlgSendLink message handlers

BOOL CDlgSendLink::OnInitDialog()
{
	CDialog::OnInitDialog();

	// set title
	CString szText;
	szText.Format( _T("Link for Request: %.8ld"), m_nItemID );
	SetWindowText( szText );

	REQUEST_SET Request;
	// get request
	appConfig.m_SQL.GetRequestSet( &Request, m_nItemID );
	// get variables
	m_nUserID = Request.m_user_id;
	m_nLibraryID = Request.m_library_id;
	m_nCollectionID = Request.m_collection_id;
	m_szDateIn = Request.m_date_in.Format( appConfig.m_General.szDateFormat );

	// set if sticky
	if( appConfig.m_General.bUseSitckyCollection )
		m_nCollectionID = AfxGetApp()->GetProfileInt( _T("Settings"), _T("StickyCollectionLink"), m_nCollectionID );
	
	INT nCurSel = 0;
	INT nSel = 0;
	INT nDefaultSel = 0;
	CString szTemp;

	// check for valid staff
	if( m_nStaffID > 0 )
	{
		// get staff
		STAFF_SET Staff;
		appConfig.m_SQL.GetStaffSet( &Staff, m_nStaffID );
		// override with default
		if( Staff.m_default_collection >= 0 )		
			m_nCollectionID = Staff.m_default_collection;
	}

	// set m_ctrlCollection
	appConfig.m_SQL.GetDescriptionCollections( &m_aCollection );

	// get first electronic for default
	for( INT nIndex = 0; nIndex < m_aCollection.GetCount(); nIndex++ )
	{
		szTemp = m_aCollection.GetAt( nIndex ).szDescription;
		if( !szTemp.IsEmpty() )
		{
			// set ID
			nSel = m_anID.Add( m_aCollection.GetAt( nIndex ).nID );
			// check for electronic
			if( m_aCollection.GetAt( nIndex ).nElectronic > 0 )
			{
				// set first colleciton
				nDefaultSel = nSel;
			}
		}
	}

	// set m_ctrlCollection list
	for( INT nIndex = 0; nIndex < m_aCollection.GetCount(); nIndex++ )
	{
		szTemp = m_aCollection.GetAt( nIndex ).szDescription;
		if( !szTemp.IsEmpty() )
		{
			// set string
			nSel = m_ctrlCollection.AddString( szTemp );
			// set to first electronic if not electronic
			if( m_nCollectionID == nIndex && m_aCollection.GetAt( nIndex ).nElectronic == 0 )
			{
				// set collection
				nCurSel = nDefaultSel;
				// set material
				m_nMaterial = 1;
			}
			else if( m_nCollectionID == nIndex && m_aCollection.GetAt( nIndex ).nElectronic > 0 )
			{
				// set collection
				nCurSel = nSel;
				// set material
				m_nMaterial = 1;
			}
		}
	}
	m_ctrlCollection.SetCurSel( nCurSel );

	// set if sticky
	if( appConfig.m_General.bUseSitckyCollection )
		m_nMaterial = AfxGetApp()->GetProfileInt( _T("Settings"), _T("StickyMaterialLink"), m_nMaterial );

	// get user
	USER_SET User;
	appConfig.m_SQL.GetUserSet( &User, m_nUserID );
	// get variables
	m_szTo = User.m_email;
	m_szInfo.Format( _T("User: %s\r\nIdentifier: %s\r\nDate In: %s"), User.m_name, Request.m_uid, m_szDateIn );

	// check for electronic
	COLLECTION_LOOKUP Collection;
	appConfig.m_SQL.GetDescriptionCollection( &Collection, Request.m_collection_id );

	// check for link
	if( !Request.m_uid.IsEmpty() )
		appConfig.m_SQL.GetRequestLinks( &m_Links, Request.m_uid );
	// Get Count
	if( m_Links.GetCount() > 0 )
	{
		m_bCheckLink = TRUE;
		m_szURL = m_Links.GetAt( 0 ).m_link;
		//if( m_szURL.IsEmpty() )
		//	GetDlgItem( IDC_BUTTON_OPEN_LINK )->SetWindowText( _T("Available Link") );
		//else
		//	GetDlgItem( IDC_BUTTON_OPEN_LINK )->SetWindowText( _T("Open Link") );
	}
	else
	{
		// disable button
		GetDlgItem( IDC_BUTTON_OPEN_LINK )->EnableWindow( FALSE );
	}

	// update data
	UpdateData( FALSE );

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgSendLink::OnOK()
{
	// update the data
	UpdateData();

	CString szTemp;

	// get collection id
	INT nIndex = m_ctrlCollection.GetCurSel();
	m_nCollectionID = m_anID.GetAt( nIndex );
	if( m_nCollectionID < 0 )
		return;

	// get library info
	LIBRARY_LOOKUP Library;
	appConfig.m_SQL.GetDescriptionLibrary( &Library, m_nLibraryID );
	m_szFrom = Library.szEmail;
	m_szName = Library.szName;
	m_szSubject = Library.szSubject;

	// check for empty URL
	if( m_szURL.IsEmpty() )
	{
		// read email file
		m_szMessage = appConfig.ReadFile( appConfig.m_Email.szNoteMessageFile );
	}
	else
	{
		// check for HTTP
		if( m_szURL.Left( 7 ).CompareNoCase( _T("http://")) != 0 )
			m_szURL = _T("http://") + m_szURL;
		// read email file
		m_szMessage = appConfig.ReadFile( appConfig.m_Email.szLinkMessageFile );
	}

	// prompt for material
	BOOL bPromptMaterial = (BOOL)AfxGetApp()->GetProfileInt( _T("Settings"), _T("PromptMaterial"), 1 );
	if( bPromptMaterial )
	{
		// get material from dialog
		CDlgPromptMaterial dlgMaterial;
		dlgMaterial.m_nMaterial = m_nMaterial;
		if( dlgMaterial.DoModal() == IDCANCEL )
			return;
		m_nMaterial = dlgMaterial.m_nMaterial;
		// update data
		UpdateData(FALSE);
	}

	// replace infomation
	m_szMessage.Replace( _T("(INFORMATION)"), m_szInformation );
	m_szMessage.Replace( _T("(CITATIONURL)"), m_szURL );

	// format message
	appConfig.FormatMacros( &m_szMessage, m_nItemID );
	// format subject macros
	appConfig.FormatMacros( &m_szSubject, m_nItemID, -1, STATUS_ID_COMPLETE );

	// show preivew
	CDlgPreviewHTML dlgPreview;
	dlgPreview.m_bSend = TRUE;
	dlgPreview.m_szHTML = m_szMessage;
	dlgPreview.m_szSubject = m_szSubject;

	// show message
	if( dlgPreview.DoModal() != IDOK )
		return;

	// get new message
	m_szMessage = dlgPreview.m_szHTML;
	m_szSubject = dlgPreview.m_szSubject;

	REQUEST_SET Request;
	// get request
	appConfig.m_SQL.GetRequestSet( &Request, m_nItemID );
	// set variables
	Request.m_type = SEND_LINK;
	Request.m_status_id = STATUS_ID_COMPLETE;
	Request.m_staff_id = m_nStaffID;
	Request.m_library_id = m_nLibraryID;
	Request.m_date_out = COleDateTime::GetCurrentTime();
	Request.m_touched = TRUE;
	Request.m_material = m_nMaterial;
	Request.m_collection_id = m_nCollectionID;
	Request.m_retain = 0;
	// add notes
	//szTemp.Format( _T("Link: <a href='%s'>%s</a><br>Information: %s"), m_szURL, m_szURL, m_szInformation );
	Request.m_notes += m_szInformation;
	Request.m_link = m_szURL;

	// update request
	appConfig.m_SQL.SetRequestSet( &Request );

	// update link table
	if( !Request.m_uid.IsEmpty() && !m_szURL.IsEmpty() )
	{
		REQUEST_LINK Link;

		Link.m_id = -1;
		Link.m_staff_id = m_nStaffID;
		Link.m_date_in = COleDateTime::GetCurrentTime();
		Link.m_uid = Request.m_uid;
		Link.m_title = Request.m_title;
		Link.m_source = Request.m_source;
		Link.m_link = m_szURL;
		Link.m_material = m_nMaterial;

		// set links
		appConfig.m_SQL.SetRequestLinks( &Link );

		// use source
		if( !Request.m_uid.IsEmpty() && !Request.m_source.IsEmpty() && appConfig.m_General.bUseAutoLinkSourcing && !appConfig.m_Options.bDisableSourceLinkCheck )
		{
			// get link sources
			CString szSourceTitle;
			szSourceTitle = Request.m_source.Left( Request.m_source.Find( appConfig.m_SFX.szSourceEndString ));
			szSourceTitle.Trim();

			// check source
			if( szSourceTitle.IsEmpty() )
				szSourceTitle = Request.m_source;				
				
			SOURCE_LINK Source;

			// check if already there
			if( !appConfig.m_SQL.GetSourceLink( &Source, _T("title"), szSourceTitle ))
			{
				// update source_link table
				Source.m_staff_id = m_nStaffID;
				Source.m_date_in = COleDateTime::GetCurrentTime();
				Source.m_title = szSourceTitle;
				Source.m_link = m_szURL;
				Source.m_material = m_nMaterial;

				// set links
				appConfig.m_SQL.SetSourceLink( &Source );
			}
		}
	}

	// write history
	appConfig.UpdateRequestHistory( Request.m_id, m_nStaffID, Request.m_status_id, _T("Sent link") );

	// send message
	CSendMessage send;
	if( !send.SendEmail( m_szName, m_szFrom, m_szTo, m_szSubject, m_szMessage ))
		return;

	// close dialog
	CDialog::OnOK();
}

void CDlgSendLink::OnCbnSelchangeComboCollection()
{
	// update the data
	UpdateData();

	// get collection id
	INT nIndex = m_ctrlCollection.GetCurSel();
	m_nCollectionID = m_anID.GetAt( nIndex );

	// set electronc
	m_nMaterial = m_aCollection.GetAt( nIndex ).nElectronic;

	// update data
	UpdateData( FALSE );
}

void CDlgSendLink::OnBnClickedButtonOpenLink()
{
	// update the data
	UpdateData();

	// check link
	if( m_bCheckLink )
	{
		if( m_Links.GetCount() == 0 )
		{
			m_szURL = m_Links.GetAt( 0 ).m_link;
		}
		else
		{
			m_szURL = m_Links.GetAt( 0 ).m_link;
		}

		// reset button
		m_bCheckLink = FALSE;
		GetDlgItem( IDC_BUTTON_OPEN_LINK )->SetWindowText( _T("Open Link") );

		// update data
		UpdateData( FALSE );
	}
	else
	{
		// open link in browser
		ShellExecute( NULL, _T("open"), m_szURL, NULL, NULL, SW_SHOWNORMAL );
	}
}

void CDlgSendLink::OnEnChangeEditUrl()
{
	// enable button
	GetDlgItem( IDC_BUTTON_OPEN_LINK )->EnableWindow();
}
